package com.example.a10928.weifriend.ui.rank_list;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a10928.weifriend.App;
import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.adapter.RecommonVipAdapter;
import com.example.a10928.weifriend.adapter.RecommondAdapter;
import com.example.a10928.weifriend.http.api.RowsWrapper;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.http.api.base.RankBean;
import com.example.a10928.weifriend.http.api.base.WeekRankBean;
import com.example.a10928.weifriend.http.api.base.YesterdayRankBean;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.ui.MainActivity;
import com.example.a10928.weifriend.util.L;
import com.example.a10928.weifriend.util.RxUtil;
import com.example.a10928.weifriend.view.SwitchLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class RankFragment extends BaseFragment {


    RecyclerView recyc_income,recyc_vip;
    SwitchLayout switchLayout;
    ArrayList<RankBean.TodayIncomeRankingBean> datas_income;
    ArrayList<RankBean.TodayRecommendRankingBean> datas;

    RecommonVipAdapter adapter;
    RecommondAdapter adapter_income;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rank, container, false);
        recyc_income=(RecyclerView)view.findViewById(R.id.recyc_income);
        recyc_vip=(RecyclerView)view.findViewById(R.id.recyc_vip);
        switchLayout=(SwitchLayout)view.findViewById(R.id.switch_layout);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        switchLayout.setSelectInterface(new SwitchLayout.SelectInterface() {
            @Override
            public void select(int index) {
                if(index==0){
                    recyc_income.setVisibility(View.VISIBLE);
                    recyc_vip.setVisibility(View.INVISIBLE);
                }else{
                    recyc_income.setVisibility(View.INVISIBLE);
                    recyc_vip.setVisibility(View.VISIBLE);
                }
            }
        });

        int type = getArguments().getInt(VariableName.TYPE);
        switch (type){
            case 0:

                yesterday();
                break;

            case 1:

                today();
                break;

            case 2:

                week();
                break;


        }


        datas_income=new ArrayList<>();
        adapter_income=new RecommondAdapter(R.layout.item_recommand_income,datas_income);
        recyc_income.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyc_income.setAdapter(adapter_income);

        datas=new ArrayList<>();
        adapter=new RecommonVipAdapter(R.layout.item_recommand_vip,datas);
        recyc_vip.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyc_vip.setAdapter(adapter);

    }


    public void yesterday(){

        ((MainActivity)getActivity()).showProgress();
        App.app().getBpService().get_yesterday_ranking(App.app().getUser().getUserId())
                .compose(RxUtil.io2main())
                .subscribe(
                        new Consumer<RowsWrapper<YesterdayRankBean>>() {
                            @Override
                            public void accept(RowsWrapper<YesterdayRankBean> rankBeanRowsWrapper) throws Exception {
                                ((MainActivity)getActivity()).dismmProgress();
                                L.v("zzw","yesterday");
                                setData(rankBeanRowsWrapper);
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                RxUtil.errorException(throwable);
                            }
                        }
                );


    }

    public void today(){
        App.app().getBpService().get_today_ranking(App.app().getUser().getUserId())
                .compose(RxUtil.io2main())
                .subscribe(
                        new Consumer<RowsWrapper<RankBean>>() {
                            @Override
                            public void accept(RowsWrapper<RankBean> rankBeanRowsWrapper) throws Exception {
                                setDataTody(rankBeanRowsWrapper);
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                RxUtil.errorException(throwable);
                            }
                        }
                );
    }

    public void week(){
        App.app().getBpService().get_week_ranking(App.app().getUser().getUserId())
                .compose(RxUtil.io2main())
                .subscribe(
                        new Consumer<RowsWrapper<WeekRankBean>>() {
                            @Override
                            public void accept(RowsWrapper<WeekRankBean> rankBeanRowsWrapper) throws Exception {

                                setDataWeek(rankBeanRowsWrapper);


                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                RxUtil.errorException(throwable);
                            }
                        }
                );
    }

    public void setDataTody(RowsWrapper<RankBean> rankBeanRowsWrapper){

        if(rankBeanRowsWrapper.getData()==null){
            return;
        }
        List<RankBean.TodayRecommendRankingBean> recommand= rankBeanRowsWrapper.getData().getToday_recommend_ranking();
        List<RankBean.TodayIncomeRankingBean> income= rankBeanRowsWrapper.getData().getToday_income_ranking();
        if(recommand!=null&&recommand.size()>0){
            for(int i=0;i<recommand.size();i++){
                recommand.get(i).setRank(i);
            }
            for(RankBean.TodayRecommendRankingBean d :recommand){
                if(d.getUser_info()!=null){
                    datas.add(d);
                }
            }
           // datas.addAll(recommand);
            adapter.notifyDataSetChanged();
        }


        if(income!=null&&income.size()>0){
            for(int i=0;i<income.size();i++){
                income.get(i).setRank(i);
            }
            for(RankBean.TodayIncomeRankingBean ddd :income){
                if(ddd.getUser_info()!=null){
                    datas_income.add(ddd);
                }
            }
           // datas_income.addAll(rankBeanRowsWrapper.getData().getToday_income_ranking());
            adapter_income.notifyDataSetChanged();
        }

    }

    public void setDataWeek(RowsWrapper<WeekRankBean> rankBeanRowsWrapper){

        if(rankBeanRowsWrapper.getData()==null){
            return;
        }
        List<RankBean.TodayRecommendRankingBean> recommand= rankBeanRowsWrapper.getData().getToday_recommend_ranking();
        List<RankBean.TodayIncomeRankingBean> income= rankBeanRowsWrapper.getData().getToday_income_ranking();
        if(recommand!=null&&recommand.size()>0){
            for(int i=0;i<recommand.size();i++){
                recommand.get(i).setRank(i);
            }
            datas.addAll(recommand);
            adapter.notifyDataSetChanged();
        }


        if(income!=null&&income.size()>0){
            for(int i=0;i<income.size();i++){
                income.get(i).setRank(i);
            }
            datas_income.addAll(rankBeanRowsWrapper.getData().getToday_income_ranking());
            adapter_income.notifyDataSetChanged();
        }

    }

    public void setData(RowsWrapper<YesterdayRankBean> rankBeanRowsWrapper){

        if(rankBeanRowsWrapper.getData()==null){
            return;
        }
        List<RankBean.TodayRecommendRankingBean> recommand= rankBeanRowsWrapper.getData().getToday_recommend_ranking();
        List<RankBean.TodayIncomeRankingBean> income= rankBeanRowsWrapper.getData().getToday_income_ranking();
        if(recommand!=null&&recommand.size()>0){
            for(int i=0;i<recommand.size();i++){
                recommand.get(i).setRank(i);
            }
            datas.addAll(recommand);
            adapter.notifyDataSetChanged();
        }


        if(income!=null&&income.size()>0){
            for(int i=0;i<income.size();i++){
                income.get(i).setRank(i);
            }
            datas_income.addAll(rankBeanRowsWrapper.getData().getToday_income_ranking());
            adapter_income.notifyDataSetChanged();
        }

    }

}
